﻿using System;
using System.Diagnostics;
using NLog;
using PostSharp.Aspects;

namespace InseSoft.Infraestructure.Aspects
{
    [Serializable]
    public class LogTiming : OnMethodBoundaryAspect
    {
        #region Declarations

        [NonSerialized]
        private static readonly Logger logger = LogManager.GetCurrentClassLogger();
        [NonSerialized]
        Stopwatch stopwatch;

        #endregion

        #region Methods: Public
        
        public override void OnEntry(MethodExecutionArgs args)
        {
            stopwatch = Stopwatch.StartNew();
            base.OnEntry(args);
        }

        public override void OnExit(MethodExecutionArgs args)
        {
            logger.Info("{0} took {1}ms to execute", args.Method.Name, stopwatch.ElapsedMilliseconds);
            base.OnExit(args);
        }
        #endregion
    }
}
